package com.example.springbootsecurityjwt.common;

/**
 * @author supan
 *
 * 通用请求返回类
 */
public class ApiResult<T> {
    /**
     * 错误码，表示一种错误类型
     * 请求成功，状态码为200
     */
    private int code;

    /**
     * 对错误代码的详细解释
     */
    private String message;

    /**
     * 返回的结果包装在value中，value可以是单个对象
     */
    private T data;

    public ApiResult() {
    }

    public ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResult(T data){
        this.data = data;
    }

    public static ApiResult success(){
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(0);
        apiResult.setMessage("成功");
        return apiResult;
}

    public static <T> ApiResult<T> success(T data){
        ApiResult apiResult = new ApiResult<>(data);
        apiResult.setCode(0);
        apiResult.setMessage("成功");
        return apiResult;
    }

    public static ApiResult error(int code,String message) {
        ApiResult apiResult = new ApiResult();
        apiResult.setCode(code);
        apiResult.setMessage(message);
        return apiResult;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
